library(ukpolice)
library(tidyverse)
Registered S3 methods overwritten by 'dbplyr':
method from
print.tbl_lazy
print.tbl_sql
-- Attaching packages ---------------------------- tidyverse 1.3.1 --
v ggplot2 3.3.5 v purrr 0.3.4
v tibble 3.1.6 v dplyr 1.0.7
v tidyr 1.2.0 v stringr 1.4.0
v readr 2.1.2 v forcats 0.5.1
-- Conflicts ------------------------------- tidyverse_conflicts() --
x dplyr::filter() masks stats::filter()
x dplyr::lag() masks stats::lag()
library(leaflet)
Registered S3 methods overwritten by 'htmltools':
method from
print.html tools:rstudio
print.shiny.tag tools:rstudio
print.shiny.tag.list tools:rstudio
Registered S3 method overwritten by 'htmlwidgets':
method from
print.htmlwidget tools:rstudio
library(leaflet.extras)
date_df <- expand.grid(year = 2020:2021,
month = 1:12)
dates <- paste0(date_df$year, "-",
str_pad(date_df$month, width = 2, pad = "0"))
dates
[1] "2020-01" "2021-01" "2020-02" "2021-02" "2020-03" "2021-03"
[7] "2020-04" "2021-04" "2020-05" "2021-05" "2020-06" "2021-06"
[13] "2020-07" "2021-07" "2020-08" "2021-08" "2020-09" "2021-09"
[19] "2020-10" "2021-10" "2020-11" "2021-11" "2020-12" "2021-12"
local_crimes_mh <- lapply(dates,
\(d) ukc_crime_coord(lat = 51.5709,
lng = -0.0960,
date = d)) %>%
bind_rows()
local_crimes_mh %>%
group_by(category) %>%
summarise(n = n()) %>%
mutate(perc = (n*100/sum(n)) %>% round(0)) %>%
arrange(desc(perc))
local_crimes_mh %>%
leaflet() %>%
addTiles() %>%
addHeatmap(
lng = ~ longitude %>% as.numeric(),
lat = ~ latitude %>% as.numeric()
)
map_crimes <- function(dat) {
dat %>%
leaflet() %>%
addTiles() %>%
addCircleMarkers(
lng = ~ longitude %>% as.numeric() %>% jitter(),
lat = ~ latitude %>% as.numeric() %>% jitter(),
popup = ~ paste0(
"<B>", category, "</B>",
"<BR>",
street_name,
"<BR>",
month,
"<BR><BR>",
"Outcome: ", outcome_status_category,
"<BR>(",
outcome_status_date, ")"
),
radius = 1
)
}
local_crimes_mh %>%
map_crimes()
num_10_crimes <- lapply(dates,
\(d) ukc_crime_coord(lat = 51.5034,
lng = -0.1276,
date = d)) %>%
bind_rows()
num_10_crimes %>%
map_crimes()